package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;
import android.os.Build;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.DefaultLayoutHelper;

import java.util.List;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.ApphomeModule1ViewBinding;
import sz.szsmk.citizencard.model.network.bean.response.QueryMainPageFunctionRes;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/12/20 14:04
 * description:
 */
public class AppHomeModule2Adapter extends DelegateAdapter.Adapter<AppHomeModule2Adapter.ApphomeModule1Holder> {

    private Context mContext;
    private LayoutHelper mGridLayoutHelper;
    private QueryMainPageFunctionRes.MainPageClassListBean viewData;
    private ApphomeModuleItemAdapter itemAdapter;


    private BaseInterface baseInterface;
    private RecyclerView moduleRv;

    public interface BaseInterface {
        public void onClick(View view, String url, String syslabl, int need_header, String modularId);
    }

    public void setBaseInterface(BaseInterface baseInterface) {
        this.baseInterface = baseInterface;
    }


    public AppHomeModule2Adapter(FragmentActivity activity, DefaultLayoutHelper headHelper) {
        mContext = activity;
        mGridLayoutHelper = headHelper;
    }

    @Override
    public int getItemViewType(int position) {
        return R.layout.apphome_module1_view;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mGridLayoutHelper;
    }

    @Override
    public ApphomeModule1Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        return ApphomeModule1Holder.creat(parent);
    }

    @Override
    public void onBindViewHolder(ApphomeModule1Holder holder, int position) {

        loadItemData(position, holder.binding);
    }

    private void loadItemData(int position, final ApphomeModule1ViewBinding binding) {
        if (viewData != null) {
            binding.apphomeModuleTitle.setText(viewData.getClass_name());
            binding.apphomeModuleTv1.setText(viewData.getMain_page_group_list().get(0).getGroup_name());
            binding.apphomeModuleTv2.setText(viewData.getMain_page_group_list().get(1).getGroup_name());
            binding.apphomeModuleTv3.setText(viewData.getMain_page_group_list().get(2).getGroup_name());
            final List<QueryMainPageFunctionRes.MainPageClassListBean.MainPageGroupListBean> main_page_group_list = viewData.getMain_page_group_list();
            moduleRv = binding.apphomeModule1Rv;
            GridLayoutManager manager = new GridLayoutManager(mContext, 2);
            moduleRv.setLayoutManager(manager);
            itemAdapter = new ApphomeModuleItemAdapter(mContext);
            itemAdapter.setBaseInterface(new ApphomeModuleItemAdapter.BaseInterface() {
                @Override
                public void onClick(View view, String url, String syslabl, int need_header, String modularId) {
                    if (baseInterface != null) {
                        baseInterface.onClick(view, url, syslabl, need_header,modularId);
                    }
                }
            });

            moduleRv.setAdapter(itemAdapter);

            int index = DaggerApplication.getBmindex();
            switch (index) {
                case 0:
                    binding.line1.setVisibility(View.VISIBLE);
                    binding.line2.setVisibility(View.GONE);
                    binding.line3.setVisibility(View.GONE);
                    itemAdapter.setViewData(viewData.getMain_page_group_list().get(0).getMain_page_function_list());
                    itemAdapter.notifyDataSetChanged();
                    moduleRv.scrollToPosition(itemAdapter.getItemCount()-1);
                    break;

                case 1:
                    itemAdapter.setViewData(viewData.getMain_page_group_list().get(1).getMain_page_function_list());
                    itemAdapter.notifyDataSetChanged();
                    binding.line1.setVisibility(View.GONE);
                    binding.line2.setVisibility(View.VISIBLE);
                    binding.line3.setVisibility(View.GONE);
                    moduleRv.scrollToPosition(itemAdapter.getItemCount()-1);
                    break;
                case 2:
                    itemAdapter.setViewData(viewData.getMain_page_group_list().get(2).getMain_page_function_list());
                    itemAdapter.notifyDataSetChanged();
                    binding.line1.setVisibility(View.GONE);
                    binding.line2.setVisibility(View.GONE);
                    binding.line3.setVisibility(View.VISIBLE);
                    moduleRv.scrollToPosition(itemAdapter.getItemCount()-1);
                    break;
            }

            binding.apphomeModuleTv1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    binding.line1.setVisibility(View.VISIBLE);
                    binding.line2.setVisibility(View.GONE);
                    binding.line3.setVisibility(View.GONE);
                    itemAdapter.setViewData(main_page_group_list.get(0).getMain_page_function_list());
                    itemAdapter.notifyDataSetChanged();
                    DaggerApplication.setBmindex(0);
                    moduleRv.scrollToPosition(itemAdapter.getItemCount()-1);
                }
            });
            binding.apphomeModuleTv2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    binding.line1.setVisibility(View.GONE);
                    binding.line2.setVisibility(View.VISIBLE);
                    binding.line3.setVisibility(View.GONE);
                    itemAdapter.setViewData(main_page_group_list.get(1).getMain_page_function_list());
                    itemAdapter.notifyDataSetChanged();
                    DaggerApplication.setBmindex(1);
                    moduleRv.scrollToPosition(itemAdapter.getItemCount()-1);
                }
            });
            binding.apphomeModuleTv3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    binding.line1.setVisibility(View.GONE);
                    binding.line2.setVisibility(View.GONE);
                    binding.line3.setVisibility(View.VISIBLE);

                    itemAdapter.setViewData(main_page_group_list.get(2).getMain_page_function_list());
                    itemAdapter.notifyDataSetChanged();
                    DaggerApplication.setBmindex(2);
                    moduleRv.scrollToPosition(itemAdapter.getItemCount()-1);
                }
            });
        }

        if(moduleRv!=null){
            moduleRv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    //设置recyclerView高度
                    int heigts = moduleRv.computeVerticalScrollRange();
                    if(heigts> DaggerApplication.getHomeryheight()){
                        DaggerApplication.setHomeryheight(heigts);
                    }


                    ViewGroup.LayoutParams layoutParams = moduleRv.getLayoutParams();
                    if (Build.VERSION.SDK_INT >= 16) {
                        moduleRv.getViewTreeObserver()
                                .removeOnGlobalLayoutListener(this);
                    } else {
                        moduleRv.getViewTreeObserver()
                                .removeGlobalOnLayoutListener(this);
                    }

//                WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
//                int height = wm.getDefaultDisplay().getHeight() / 2;

//                    layoutParams.height = height/2;
                    layoutParams.height = DaggerApplication.getHomeryheight();
                    moduleRv.setLayoutParams(layoutParams);

                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    public void setViewData(QueryMainPageFunctionRes.MainPageClassListBean viewData) {
        this.viewData = viewData;
    }

    public static class ApphomeModule1Holder extends RecyclerView.ViewHolder {

        private ApphomeModule1ViewBinding binding;

        public static ApphomeModule1Holder creat(ViewGroup parent) {
            ApphomeModule1ViewBinding binding =
                    ApphomeModule1ViewBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new ApphomeModule1Holder(binding);
        }


        public ApphomeModule1Holder(ApphomeModule1ViewBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

}
